﻿Imports System.Data.Entity
Imports System.Data.Entity.Infrastructure
Imports System.Linq
Imports System.Net
Imports System.Threading.Tasks
Imports System.Web.Http
Imports System.Web.Http.Description

Namespace Controllers.ApiControllers
    Public Class CountriesController
        Inherits ApiController

        Private db As New UserInfoEntities

        ' GET: api/Countries
        Function GetCountries() As IQueryable(Of Country)
            Return db.Countries
        End Function

        ' GET: api/Countries/5
        <ResponseType(GetType(Country))>
        Async Function GetCountry(ByVal id As String) As Task(Of IHttpActionResult)
            Dim country As Country = Await db.Countries.FindAsync(id)
            If IsNothing(country) Then
                Return NotFound()
            End If

            Return Ok(country)
        End Function

        ' PUT: api/Countries/5
        <ResponseType(GetType(Void))>
        Async Function PutCountry(ByVal id As String, ByVal country As Country) As Task(Of IHttpActionResult)
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            If Not id = country.id Then
                Return BadRequest()
            End If

            db.Entry(country).State = EntityState.Modified

            Try
                Await db.SaveChangesAsync()
            Catch ex As DbUpdateConcurrencyException
                If Not (CountryExists(id)) Then
                    Return NotFound()
                Else
                    Throw
                End If
            End Try

            Return StatusCode(HttpStatusCode.NoContent)
        End Function

        ' POST: api/Countries
        <ResponseType(GetType(Country))>
        Async Function PostCountry(ByVal country As Country) As Task(Of IHttpActionResult)
            If Not ModelState.IsValid Then
                Return BadRequest(ModelState)
            End If

            db.Countries.Add(country)

            Try
                Await db.SaveChangesAsync()
            Catch ex As DbUpdateException
                If (CountryExists(country.id)) Then
                    Return Conflict()
                Else
                    Throw
                End If
            End Try

            Return CreatedAtRoute("DefaultApi", New With {.id = country.id}, country)
        End Function

        ' DELETE: api/Countries/5
        <ResponseType(GetType(Country))>
        Async Function DeleteCountry(ByVal id As String) As Task(Of IHttpActionResult)
            Dim country As Country = Await db.Countries.FindAsync(id)
            If IsNothing(country) Then
                Return NotFound()
            End If

            db.Countries.Remove(country)
            Await db.SaveChangesAsync()

            Return Ok(country)
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing) Then
                db.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub

        Private Function CountryExists(ByVal id As String) As Boolean
            Return db.Countries.Count(Function(e) e.id = id) > 0
        End Function
    End Class
End Namespace